<?php
function reequipment_adminapi_updatemaintenance($args)
{
    // Get arguments from argument array
    extract($args);

    /* Fields in table.
		maintenance_id
		equipment_id
		company_id
		schedule_id
		frequency_id
		date_scheduled
		date_complete
		issue
		resolution
		item_ids
		manhours
		downtime
   */
   
	// Set as complete if a resolution is entered.
	// This probably needs to be done a bit more elegantly as anytime you edit a completed work order, it changes the time.
	if (!empty($resolution)){
		$date_complete = time();
	}   

    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['remaintenance'];
    $update = array();
    $bindvars = array();

    // Update item
    $query = "UPDATE $table SET ";
    if (isset($maintenance_id)){
        $update[] = "maintenance_id = ? ";
        $bindvars[] = $maintenance_id;
    }
    if (isset($equipment_id)){
        $update[] = "equipment_id = ? ";
        $bindvars[] = $equipment_id;
    }
    if (isset($company_id)){
        $update[] = "company_id = ? ";
        $bindvars[] = $company_id;
    }
    if (isset($schedule_id)){
        $update[] = "schedule_id = ? ";
        $bindvars[] = $schedule_id;
    }
    if (isset($frequency_id)){
        $update[] = "frequency_id = ? ";
        $bindvars[] = $frequency_id;
    }
    if (isset($date_scheduled)){
        $update[] = "date_scheduled = ? ";
        $bindvars[] = $date_scheduled;
    }
    if (isset($date_due)){
        $update[] = "date_due = ? ";
        $bindvars[] = $date_due;
    }
    if (isset($cycles_due)){
        $update[] = "cycles_due = ? ";
        $bindvars[] = $cycles_due;
    }
    if (isset($date_complete)){
        $update[] = "date_complete = ? ";
        $bindvars[] = $date_complete;
    }
    if (isset($issue)){
        $update[] = "issue = ? ";
        $bindvars[] = $issue;
    }
    if (isset($resolution)){
        $update[] = "resolution = ? ";
        $bindvars[] = $resolution;
    }
    if (isset($item_ids)){
        $update[] = "item_ids = ? ";
        $bindvars[] = $item_ids;
    } 
    if (isset($manhours)){
        $update[] = "manhours = ? ";
        $bindvars[] = $manhours;
    }        
    if (isset($downtime)){
        $update[] = "downtime = ? ";
        $bindvars[] = $downtime;
    }       
    $query .= join(",",$update);
    $query .= "WHERE maintenance_id = ? ";
    $bindvars[] = $maintenance_id;
    $result =& $dbconn->Execute($query, $bindvars);
    if (!$result) return;
    // Let any hooks know that we have created a new topic
    $args['module'] = 'reequipment';
    $args['itemtype'] = 6; //tag
    $args['itemid'] = $maintenance_id;
    xarModCallHooks('item', 'update', $maintenance_id, $args);
    
    // If this was a scheduled maintenance, open the next instance
    if ((isset($maintenance_id)) AND (isset($date_complete))){
    	// Get this maintenance action
    	$m = xarModAPIFunc('reequipment', 'user', 'getmaintenance', array('maintenance_id' => $maintenance_id));
    	// return var_export($m);
    	foreach ($m as $var){
    		// Only checked for scheduled maintenance
    		if (!empty($var['schedule_id']) and ($var['rel_frequency_id'] == '0')){
				$due = xarModAPIFunc('reequipment', 'user', 'determinefrequency', array('frequency' => $var['frequency_name'], 'cycles' => $var['cycles']));
				if (!empty($due['date'])){
					// return var_export($due['date']);
					$postinfo = xarModAPIFunc('reequipment', 'admin', 'createmaintenance', array('frequency_id' => 0, 'schedule_id' => intval($var['schedule_id']), 'issue' => $var['issue'], 'equipment_id' => intval($var['equipment_id']), 'company_id' => intval($var['company_id']), 'date_due' => intval($due['date'])));
				} elseif (!empty($due['cycles'])) {
					$postinfo = xarModAPIFunc('reequipment', 'admin', 'createmaintenance', array('frequency_id' => 0, 'schedule_id' => intval($var['schedule_id']), 'issue' => $var['issue'], 'equipment_id' => intval($var['equipment_id']), 'company_id' => intval($var['company_id']), 'cycles_due' => intval($due['cycles'])));
				}
			} elseif ($var['rel_frequency_id'] != '0') {
				$due = xarModAPIFunc('reequipment', 'user', 'determinefrequency', array('frequency' => $var['rel_frequency_name'], 'cycles' => $var['cycles']));
				if (!empty($due['date'])){
					// return var_export($due['date']);
					$postinfo = xarModAPIFunc('reequipment', 'admin', 'createmaintenance', array('frequency_id' => intval($var['rel_frequency_id']), 'schedule_id' => intval($var['schedule_id']), 'issue' => $var['issue'], 'equipment_id' => intval($var['equipment_id']), 'company_id' => intval($var['company_id']), 'date_due' => intval($due['date'])));
				} elseif (!empty($due['cycles'])) {
					$postinfo = xarModAPIFunc('reequipment', 'admin', 'createmaintenance', array('frequency_id' => intval($var['rel_frequency_id']), 'schedule_id' => intval($var['schedule_id']), 'issue' => $var['issue'], 'equipment_id' => intval($var['equipment_id']), 'company_id' => intval($var['company_id']), 'cycles_due' => intval($due['cycles'])));
				}			
			}
		}
	}
    
    // Need to delete the cycle relationship and recreate it.
    $cycles = xarModAPIFunc('reequipment', 'admin', 'deleterelcycles', array('maintenance_id' => $maintenance_id));
    $cycles = xarModAPIFunc('reequipment', 'admin', 'createrelcycles', array('equipment_id' => $equipment_id, 'maintenance_id' => $maintenance_id));
    
    return true;
}
?>